import { Component } from '@angular/core';
import { userCenterService } from '../../service/userCenter.service';
import { isBindPNService } from '../../service/isBindPN.service';

declare let $:any;

@Component({
    selector: 'member_show',
    templateUrl: '../../../views/user_center/userCenter.html',
    styleUrls: ['../../../css/user_center/userCenter.css'],
    providers: [
        userCenterService,
        isBindPNService
    ]
})

export class UserCenterComponent {
    public id: any;
    public username: any;
    public email: any;
    public mobile: any;
    public nickname: any;
    public sex:any;
    public sexN:any;
    public avatar: any;
    public avatar_path: any;
    public project_count: any;
    public money: any;

    public isModify:any = false;
    public original: any;
    public password: any;
    public agin_password: any;


    constructor(
        private userCenterService : userCenterService,
        private isB : isBindPNService
    ){
        this.getUserMsg();
        this.isB.isId();
        this.isB.isBindPN();
        $.cookie("home","5");
    }
    // 用户信息
    private  getUserMsg(){
        let self : any = this;
        self.id = $.cookie("id");
        console.log(self.id);
        let data : any = {
            member_id: self.id,
        };
        self.userCenterService.userMsg({
            data: data,
            success: function (result : any) {
                self.email = result.email;
                self.mobile = result.mobile;
                self.username = result.username;
                self.nickname = result.nickname;
                if(result.nickname == null || result.nickname == ""){
                    self.nickname = "请填写昵称";
                }else {
                    self.nickname = result.nickname;
                }
                self.avatar = result.avatar;
                self.avatar_path = result.avatar_path;
                self.project_count = result.project_count;
                self.money = result.money;
                if(result.sex == 1){
                    self.sex ="男"
                }else {
                    self.sex = "女"
                }

            }
        });
    }

    public modifyMsg(){
        this.isModify = true;
        console.log(this.isModify);
        this.userEidtMsg();
    }

    private  userEidtMsg(){
        let self : any = this;
        self.id = $.cookie("id");
        console.log(self.id);
        let data : any = {
            member_id: self.id,
        };
        self.userCenterService.userEditMsg({
            data: data,
            success: function (result : any) {
                console.log(result);
                self.email = result.email;
                self.mobile = result.mobile;
                self.nickname = result.nickname;
                if(result.nickname == null || result.nickname == ""){
                    self.nickname = "请填写昵称";
                }else {
                    self.nickname = result.nickname;
                }

                if (result.sex == 1){
                    $("#optionsRadios1").attr("checked",true);
                    $("#optionsRadios1_1").attr("checked",true);
                    self.sexN = "1";
                }else {
                    $("#optionsRadios2").attr("checked",true);
                    $("#optionsRadios2_1").attr("checked",true);
                    self.sexN = "2";
                }
                console.log(this.sexN);
            }
        });
    }


    // 修改用户信息
    public modifyUser_msg(){
        this.modifyUserMsg();
    }
    public sexM(){
        this.sex = "男";
        this.sexN = "1";
        console.log(this.sex);
    }
    public sexW(){
        this.sex = "女";
        this.sexN = "2";
        console.log(this.sex);
    }
    private modifyUserMsg(){
        let self : any = this;
        self.id = $.cookie("id");
        let datajson: any;
        datajson = {
            "email": self.email,
            "nickname":self.nickname,
            "sex": self.sexN
        };
        console.log(datajson);
        let jsonData:any = JSON.stringify(datajson);
        console.log(jsonData);
        let data : any = {
            member_id: self.id,
            data: jsonData
        };
        self.userCenterService.modifyUserMsg({
            data: data,
            success: function (result : any) {
                console.log(result);
                if(result.error === 0){
                    self.getUserMsg();
                    self.isModify = false;
                }
            }
        });
    }

    // 修改密码
    public modifyPass_word(){
        this.modifyPassword();
    }
    private modifyPassword(){
        let self : any = this;
        self.id = $.cookie("id");
        let data : any = {
            member_id: self.id,
            original: self.original,
            password: self.password,
            agin_password: self.agin_password,
        };
        self.userCenterService.modifyUserPassword({
            data: data,
            success: function (result : any) {
                console.log(result)
                if(result.error === 0){
                    self.getUserMsg();
                    self.isModify = false;
                    self.original = "";
                    self.password = "";
                    self.agin_password = "";
                }
            }
        });
    }
}

